//
//  ViewController.m
//  GLModelExample
//
//  Created by Nick Lockwood on 20/12/2011.
//  Copyright (c) 2011 __MyCompanyName__. 
//

#import "ViewController.h"


@implementation ViewController

@synthesize navBar;
@synthesize modelView;

- (void)viewDidUnload
{
    [super viewDidUnload];
    self.navBar = nil;
    self.modelView = nil;
}

- (void)setModel:(NSInteger)index
{
//    定义变量，使模型随时间变化旋转
//    float x,y,z;
    
    switch (index)
    {
        case 0:
        {
            //set title
            navBar.topItem.title = @"demon.model";
            
            //set model
            modelView.texture = [GLImage imageNamed:@"demon.png"];
            modelView.blendColor =nil ;
            modelView.model = [GLModel modelWithContentsOfFile:@"demon.model"];
            
            //set default transform
            CATransform3D transform = CATransform3DMakeTranslation(0.0f, 0.0f, -2.0f);
            transform = CATransform3DScale(transform, 0.01f, 0.01f, 0.01f);
            transform = CATransform3DRotate(transform, -M_PI_2/2, 1.0f, -0.0f, -0.0f);
            modelView.transform = transform;
            
            
            break;
        }
        case 1:
        {
            //set title
            navBar.topItem.title = @"quad";
            
            //set model
            modelView.texture = nil;
            modelView.blendColor = [UIColor redColor];
            modelView.model = [GLModel modelWithContentsOfFile:@"quad.obj"];
            
            //set default transform
            modelView.transform = CATransform3DMakeTranslation(0.0f, 0.0f, -2.0f);
            
            break;
        }
        case 2:
        {
            //set title
            navBar.topItem.title = @"chair.obj";
            
            //set model
//            modelView.texture = [GLImage imageNamed:@"chair.tga"];
            modelView.blendColor = nil;
            modelView.model = [GLModel modelWithContentsOfFile:@"car.obj"];
            
            //set default transform
            CATransform3D transform = CATransform3DMakeTranslation(0.0f, 0.0f, -25.0f);
            transform = CATransform3DScale(transform, 0.01f, 0.01f, 0.01f);
            transform = CATransform3DRotate(transform, 0.2f, 1.0f, 1.0f, 1.0f);
            modelView.transform = transform;
            
            break;
        }
        case 3:
        {
            //set title
            navBar.topItem.title = @"diamond.obj";
            
            //set model
            modelView.texture = nil;
            modelView.blendColor = [UIColor greenColor];
            modelView.model = [GLModel modelWithContentsOfFile:@"diamond.obj"];
            
            //set default transform
            CATransform3D transform = CATransform3DMakeTranslation(0.0f, 0.0f, -1.0f);
            transform = CATransform3DScale(transform, 0.01f, 0.01f, 0.01f);
            transform = CATransform3DRotate(transform, M_PI_2, 1.0f, 0.0f, 0.0f);
            modelView.transform = transform;
            
            break;
        }
        case 4:
        {
            //set title
            navBar.topItem.title = @"cube.obj";
            
            //set model
            modelView.texture = nil;
            modelView.blendColor = [UIColor redColor];
            modelView.model = [GLModel modelWithContentsOfFile:@"cube.obj"];
            
            //set default transform
            CATransform3D transform = CATransform3DMakeTranslation(0.0f, 0.0f, -1.0f);
            transform = CATransform3DRotate(transform, M_PI_4, 1.0f, 1.0f, 0.0f);
            modelView.transform = transform;
            
            break;
        }
        case 5:
        {
            //set title
            navBar.topItem.title = @"ship.obj";
            
            //set model
            modelView.texture = nil;
            modelView.blendColor = [UIColor grayColor];
            modelView.model = [GLModel modelWithContentsOfFile:@"ship.obj"];
            
            //set default transform
            CATransform3D transform = CATransform3DMakeTranslation(0.0f, 0.0f, -15.0f);
            transform = CATransform3DRotate(transform, M_PI + 0.4f, 0.0f, 0.0f, 1.0f);
            transform = CATransform3DRotate(transform, M_PI_4, 1.0f, 0.0f, 0.0f);
            transform = CATransform3DRotate(transform, -0.4f, 0.0f, 1.0f, 0.0f);
            transform = CATransform3DScale(transform, 3.0f, 3.0f, 3.0f);
            modelView.transform = transform;
            
            break;
        }
    }
}

- (void)selectModel
{
    [[[UIActionSheet alloc] initWithTitle:nil
                                 delegate:self
                        cancelButtonTitle:nil
                   destructiveButtonTitle:nil
                        otherButtonTitles:@"Demon", @"Quad", @"Chair", @"Diamond", @"Cube", @"Ship", nil] showInView:self.view];
}

- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    if (buttonIndex >= 0)
    {
        [self setModel:buttonIndex];
    }
}

- (void)viewDidLoad
{
    [self setModel:0];
//    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"rotate" style:UIBarButtonItemStylePlain target:self action:@selector(rotate3dmodel)];
    self.rat = 1.5;

}

- (IBAction)plusX:(id)sender {
    self.ratX -= 0.1;
    CATransform3D transform = CATransform3DMakeTranslation(0.0f, 0.0f, -2.0f);
    transform = CATransform3DScale(transform, 0.01f, 0.01f, 0.01f);
    transform = CATransform3DRotate(transform, -self.rat, self.ratX, -0.0f, -0.0f);
    modelView.transform = transform;
}
- (IBAction)plusY:(id)sender {
    CATransform3D transform = CATransform3DMakeTranslation(0.0f, 0.0f, -2.0f);
    transform = CATransform3DScale(transform, 0.01f, 0.01f, 0.01f);
    transform = CATransform3DRotate(transform, -self.rat, 1.0f, -0.0f, -0.0f);
    modelView.transform = transform;
}

- (IBAction)rotate3d:(id)sender {
    self.rat -= 0.1;
    CATransform3D transform = CATransform3DMakeTranslation(0.0f, 0.0f, -2.0f);
    transform = CATransform3DScale(transform, 0.01f, 0.01f, 0.01f);
    transform = CATransform3DRotate(transform, -self.rat, 1.0f, -0.0f, -0.0f);
    modelView.transform = transform;

}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    
    startPoint = [touch locationInView:self.view];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    
    CGPoint currentPosition = [touch locationInView:self.view];
    
    CGFloat deltaX = startPoint.x - currentPosition.x;
    
    CGFloat deltaY = startPoint.y - currentPosition.y;
    
    CATransform3D c1t = CATransform3DMakeTranslation(0.0f, 0.0f, -5.0f);
//    CATransform3D c1t = CATransform3DIdentity;
    c1t  = CATransform3DScale(c1t, 0.01f, 0.01f, 0.01f);
//    c1t = CATransform3DTranslate(c1t, 0, 0, 0);
    c1t = CATransform3DRotate(c1t, pix+M_PI_2*deltaY/100, 1, 0, 0);
    c1t = CATransform3DRotate(c1t, piy-M_PI_2*deltaX/100, 0, 1, 0);
    
    modelView.transform = c1t;
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    
    CGPoint currentPosition = [touch locationInView:self.view];
    
    CGFloat deltaX = startPoint.x - currentPosition.x;
    
    CGFloat deltaY = startPoint.y - currentPosition.y;
    
    pix = M_PI_2*deltaY/100;
    piy = -M_PI_2*deltaX/100;
}




@end
